<template>
  <div class="swiper-container" ref="cur">
    <div class="swiper-wrapper" ref="cur">
      <div
        class="swiper-slide"
        v-for="(carouse, index) in list"
        :key="carouse.id"
      >
        <img :src="carouse.imgUrl" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
export default {
  name: "Carousel",
  props: ["list"],
  watch: {
    list: {
      // 立即监听:不管你数据有没有变化,我上来立即监听一次
      // 为什么watch监听不到,因为传送过来的list数据没有变化
      immediate: true,
      handler() {
        // 只能监听到数据已经有了,但是v-for动态渲染结构我们还是没有办法确定的,因此还是需要nextTick
        this.$nextTick(() => {
          var mySwiper = new Swiper(this.$refs.cur, {
            // direction: 'vertical', // 垂直切换选项
            loop: true, // 循环模式选项
            // 如果需要分页器
            pagination: {
              el: ".swiper-pagination",
              // 点击小球也滚动图片
              clickable: true,
            },
            // 如果需要前进后退按钮
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
    },
  },
};
</script>

<style lang="less" scoped>
</style>